<template>
  <div class="page"
       :style="{width:width+'px',height:height+'px'}">
    <div class="blank"></div>
    <div v-if="questionList.length">
      <div class="question"
           v-for="(item,index) in questionList"
           :key=index>
        <div class="questionNum">
          第{{index+1}}题
        </div>
        <div class="questionContent">
          <div class="questionTitle">
            {{questionList[index].question}}
          </div>
          <div class="questionAnswer"
               :data-answer=item.answer>
            <div v-for="(value,key2,index2) in answer_list2[index]"
                 :key=index2>
              <p @click.stop="choise(index,key2,index2)"
                 :class='{"right":questionList[index].activeIndex*1 ===key2}'>
                <span>{{options[key2]}}</span>
                {{value}}
              </p>
            </div>
          </div>
        </div>
        <div class="answerTips"
             :data-id=getPaperid>
          <div>
            <span :data-ids=rightAnswerArray[i]
                  v-if="(rightAnswerArray[index]===questionList[index].res)"
                  style="color:#00cc76">回答正确</span>
            <span v-else>回答错误</span>
          </div>
        </div>
        <div class="analyseAnswer">
          <p>正确答案：{{questionList[index].answerkey||questionList[index].answer}}</p>
          <p>所在章节：{{questionList[index].location}} </p>
          <p>知识点： {{questionList[index].point}} </p>
          <p>答案解析：</p>
          <p>{{questionList[index].analysis}}</p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      tabs: ['单选题', '多选题', '判断题', '填空题'],
      options: ['A', 'B', 'C', 'D'],
      activeIndex: 0,
      questionList: [],
      index: 0,
      getPaperid: -1,
      answer_list2: [],
      rightAnswerArray: [],
      noData: false
    }
  },
  computed: {
    ...mapState({
      examPaper: state => state.examPaper,
      randomRecord: state => state.randomRecord,
      formalRecord: state => state.formalRecord,
      userInfo: state => state.userInfo
    }),
    navbarSliderClass () {
      if (Number(this.activeIndex) === 0) {
        return 'weui-navbar__slider_0'
      }
      if (Number(this.activeIndex) === 1) {
        return 'weui-navbar__slider_1'
      }
      if (Number(this.activeIndex) === 2) {
        return 'weui-navbar__slider_2'
      }
      if (Number(this.activeIndex) === 3) {
        return 'weui-navbar__slider_3'
      }
    },
    answer_list () {
      let that = this
      if (that.questionList.length) {
        for (let i = 0; i < that.questionList.length; i++) {
          if (that.questionList[i].answerlist) {
            return Object.keys(that.questionList[i].answerlist).map(key => that.questionList[i].answerlist[key]).sort((a, b) => {
              return a.localeCompare(b)
            })
          } else {
            console.log(i)
            return Object.keys(that.questionList[i].answer_list).map(key => that.questionList[i].answer_list[key]).sort((a, b) => {
              return a.localeCompare(b)
            })
          }
        }
      }
    }
  },
  methods: {
    tabClick (e) {
      if (Number(e)) {
        this.activeIndex = 0
      } else {
        this.activeIndex = e.currentTarget.id
      }
      console.log(e.currentTarget.id)
      wx.showLoading({
        title: '加载中',
        success () {
          wx.hideLoading()
        }
      })
    },
    init (paperId) {
      let that = this
      this.questionList = []
      this.userid = this.userInfo.user_id || wx.getStorageSync('user_id')
      console.log(this.userid)
      wx.showLoading({
        title: 'loading'
      })
      this.$http.post('https://dati.co-links.com/operat/red_fullrecording/', {
        userid: that.userid,
        recordingid: paperId
      }).then((res) => {
        console.log(res)
        that.questionList = res.data.data
        wx.hideLoading()
        for (let i in that.questionList) {
          that.rightAnswerArray[i] = that.options.indexOf(that.questionList[i].answerkey)
          that.answer_list2[i] = that.questionList.length && Object.keys(that.questionList[i].answerlist).map(key => that.questionList[i].answerlist[key]).sort((a, b) => {
            return a.localeCompare(b)
          })
        }
      })
    }
  },
  mounted () {
    this.userid = this.userInfo.user_id || wx.getStorageSync('user_id')
    this.getPaperid = this.$mp.query.paperid
    if (this.$mp.query.type === 'formal') {
      if (this.$mp.query.paperid >= 0) {
        this.init(this.$mp.query.paperid)
      } else {
        this.questionList = this.formalRecord.paperTest
        for (let i in this.questionList) {
          this.rightAnswerArray[i] = this.options.indexOf(this.questionList[i].answer)
          this.answer_list2[i] = this.questionList.length && Object.keys(this.questionList[i].answer_list).map(key => this.questionList[i].answer_list[key]).sort((a, b) => {
            return a.localeCompare(b)
          })
        }
        console.log(this.answer_list2)
        console.log(this.questionList)
        console.log(this.rightAnswerArray)
      }
    } else if (this.$mp.query.type === 'random') {
      if (this.$mp.query.paperid >= 0) {
        this.init(this.$mp.query.paperid)
      } else {
        this.questionList = this.formalRecord.paperTest
        for (let i in this.questionList) {
          this.rightAnswerArray[i] = this.options.indexOf(this.questionList[i].answer)
          this.answer_list2[i] = this.questionList.length && Object.keys(this.questionList[i].answer_list).map(key => this.questionList[i].answer_list[key]).sort((a, b) => {
            return a.localeCompare(b)
          })
        }
      }
    } else {
      console.log(this.$mp.query.paperid)
      this.init(this.$mp.query.paperid)
    }
    console.log(this.questionList)
    console.log(this.examPaper.rightAnswerArray)
  }
}
</script>
<style lang="stylus" scoped>
.page {
  background: #f7f7f7;
}

.weui-navbar {
  border-bottom: 0;
  background: white;
}

.question {
  margin-bottom: 20rpx;
  overflow: hidden;

  .questionNum {
    position: relative;
    padding: 0 32rpx;
    font-size: 30rpx;
    color: #333;
    height: 98rpx;
    line-height: 98rpx;
    background: white;

    &:after {
      position: absolute;
      content: '';
      left: 32rpx;
      bottom: 0;
      right: 32rpx;
      height: 2rpx;
      width: 100rpx;
      // background #e6e6e6
      background: red;
    }
  }

  .questionNum:after {
    position: absolute;
    content: '';
    color: red;
    left: 32rpx;
    bottom: 10rpx;
    right: 32rpx;
    height: 2rpx;
    width: 100rpx;
    // background #e6e6e6
    background: red;
  }

  .questionTitle {
    font-size: 32rpx;
    color: #333333;
    padding: 20rpx 32rpx;
    background: white;
  }
}

.answerTips {
  padding: 0 12rpx;
  background: white;
  color: #ef5151;
  height: 80rpx;
  line-height: 80rpx;
  border-radius: 16rpx;
  font-size: 30rpx;
  margin: 20rpx 32rpx 28rpx 32rpx;
}

.questionContent {
  padding: 0 32rpx;
  padding-bottom: 30rpx;
  background: white;
}

.questionAnswer {
  // padding: 0 32rpx;
  p {
    font-size: 32rpx;
    color: #666666;
    padding: 18rpx 0;
    padding-left: 30rpx;

    span {
      display: inline-block;
      width: 48rpx;
      height: 48rpx;
      border-radius: 50%;
      margin-right: 30rpx;
      border: 1px solid #a5a5a5;
      text-align: center;
      line-height: 48rpx;
    }
  }
}

.analyseAnswer {
  padding: 20rpx 22rpx;
  background: #fff;
  font-size: 32rpx;
  color: #333333;
  border-radius: 12rpx;
  -webkit-border-radius: 12rpx;
  margin: 0 32rpx 20rpx 32rpx;

  p {
    line-height: 60rpx;
  }

  p:last-child {
    font-size: 28rpx;
    white-space: normal;
  }
}
</style>
